home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH12 / SRC / OBJPICT1.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  7.9 KB  |  303 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjPicture"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Public Objects As New Collection
  11.  
  12. Const TYPE_STRING = "3D APF PICTURE"
  13.  
  14. ' ************************************************
  15. ' Return the distance from the picture to a point.
  16. ' ************************************************
  17. Property Get Distance(X As Single, Y As Single, z As Single) As Single
  18. Dim best As Single
  19. Dim dist As Single
  20. Dim obj As Object
  21.        
  22.     best = INFINITY
  23.     For Each obj In Objects
  24.         dist = obj.Distance(X, Y, z)
  25.         If best > dist Then best = dist
  26.     Next obj
  27.     Distance = best
  28. End Property
  29.  
  30.  
  31. ' ***********************************************
  32. ' Create normals for polygon objects.
  33. ' ***********************************************
  34. Sub CreateNormal()
  35. Dim obj As Object
  36.  
  37.     For Each obj In Objects
  38.         If obj.ObjectType = "SOLID" Or _
  39.            obj.ObjectType = "POLYGON" Then _
  40.                 obj.CreateNormal Objects
  41.     Next obj
  42. End Sub
  43.  
  44.  
  45. ' ************************************************
  46. ' Draw the transformed picture on a Form, Printer,
  47. ' or PictureBox. Draw the faces in depth-sort
  48. ' order using polygon shading.
  49. ' ************************************************
  50. Public Sub DrawShaded(canvas As Object, Optional r As Variant)
  51. Dim ordered As New Collection
  52. Dim obj As Object
  53. Dim besti As Integer
  54. Dim bestz As Single
  55. Dim newz As Single
  56. Dim i As Integer
  57.  
  58.     ' Compute each object's Zmax value.
  59.     For Each obj In Objects
  60.         If obj.ObjectType = "SOLID" Or _
  61.            obj.ObjectType = "TRANSFORMED" _
  62.             Then obj.SetZmax
  63.     Next obj
  64.     
  65.     ' Sort the objects by their Zmax values.
  66.     Do While Objects.Count > 0
  67.         ' Find the face with the smallest Zmax
  68.         ' left in the Faces collection.
  69.         besti = 1
  70.         bestz = Objects.Item(1).zmax
  71.         For i = 2 To Objects.Count
  72.             newz = Objects.Item(i).zmax
  73.             If bestz > newz Then
  74.                 besti = i
  75.                 bestz = newz
  76.             End If
  77.         Next i
  78.         
  79.         ' Add the best object to the sorted list.
  80.         ordered.Add Objects.Item(besti)
  81.         Objects.Remove besti
  82.     Loop
  83.     
  84.     ' Replace the Objects collection with the
  85.     ' ordered collection.
  86.     Set Objects = ordered
  87.  
  88.     ' Draw the objects in sorted order.
  89.     For Each obj In Objects
  90.         obj.DrawShaded canvas, r
  91.     Next obj
  92. End Sub
  93.  
  94.  
  95. ' ************************************************
  96. ' Draw the transformed picture on a Form, Printer,
  97. ' or PictureBox. Draw the faces in depth-sort
  98. ' order.
  99. ' ************************************************
  100. Public Sub DrawOrdered(canvas As Object, Optional r As Variant)
  101. Dim ordered As New Collection
  102. Dim obj As Object
  103. Dim besti As Integer
  104. Dim bestz As Single
  105. Dim newz As Single
  106. Dim i As Integer
  107.  
  108.     ' Compute each object's Zmax value.
  109.     For Each obj In Objects
  110.         If obj.ObjectType = "SOLID" Or _
  111.            obj.ObjectType = "TRANSFORMED" _
  112.             Then obj.SetZmax
  113.     Next obj
  114.     
  115.     ' Sort the objects by their Zmax values.
  116.     Do While Objects.Count > 0
  117.         ' Find the face with the smallest Zmax
  118.         ' left in the Faces collection.
  119.         besti = 1
  120.         bestz = Objects.Item(1).zmax
  121.         For i = 2 To Objects.Count
  122.             newz = Objects.Item(i).zmax
  123.             If bestz > newz Then
  124.                 besti = i
  125.                 bestz = newz
  126.             End If
  127.         Next i
  128.         
  129.         ' Add the best object to the sorted list.
  130.         ordered.Add Objects.Item(besti)
  131.         Objects.Remove besti
  132.     Loop
  133.     
  134.     ' Replace the Objects collection with the
  135.     ' ordered collection.
  136.     Set Objects = ordered
  137.     
  138.     ' Draw the objects in sorted order.
  139.     For Each obj In Objects
  140.         obj.DrawOrdered canvas, r
  141.     Next obj
  142. End Sub
  143.  
  144.  
  145. Property Let Culled(value As Boolean)
  146. Dim obj As Object
  147.  
  148.     For Each obj In Objects
  149.         obj.Culled = value
  150.     Next obj
  151. End Property
  152.  
  153.  
  154.  
  155.  
  156. ' ************************************************
  157. ' Find an object that contains this point.
  158. ' ************************************************
  159. Function NearestObject(X As Single, Y As Single) As Object
  160. Dim obj As Object
  161.        
  162.     ' Find the object.
  163.     For Each obj In Objects
  164.         If obj.Contains(X, Y) Then
  165.             Set NearestObject = obj
  166.             Exit Function
  167.         End If
  168.     Next obj
  169.     Set NearestObject = Nothing
  170. End Function
  171.  
  172.  
  173. Function ObjectType() As String
  174.     ObjectType = TYPE_STRING
  175. End Function
  176.  
  177.  
  178. ' ************************************************
  179. ' Save the objects in the picture into a metafile.
  180. ' ************************************************
  181. Sub MakeWMF(mhdc As Integer)
  182. Dim obj As Object
  183.  
  184.     For Each obj In Objects
  185.         obj.MakeWMF mhdc
  186.     Next obj
  187. End Sub
  188.  
  189. ' ************************************************
  190. ' Read the picture from a file using Input.
  191. ' Assume TYPE_STRING has already been read.
  192. ' ************************************************
  193. Sub FileInput(filenum As Integer)
  194. Dim num As Integer
  195. Dim i As Integer
  196. Dim obj As Object
  197. Dim obj_type As String
  198.  
  199.     ' Read the number of objects in the file.
  200.     Input #filenum, num
  201.     
  202.     ' Repeatedly read objects from the file.
  203.     For i = 1 To num
  204.         Input #filenum, obj_type
  205.         Select Case obj_type
  206.             Case TYPE_STRING
  207.                 Set obj = New ObjPicture
  208.             Case "SOLID"
  209.                 Set obj = New ObjSolid
  210.             Case Else
  211.                 Beep
  212.                 MsgBox "Unknown object type """ & obj_type & """.", , vbExclamation
  213.                 Exit Sub
  214.         End Select
  215.         obj.FileInput filenum
  216.         Objects.Add obj
  217.     Next i
  218. End Sub
  219.  
  220. ' ************************************************
  221. ' Draw the picture on a Form, Printer, or
  222. ' PictureBox.
  223. ' ************************************************
  224. Sub Draw(canvas As Object, Optional r As Variant)
  225. Dim obj As Object
  226.  
  227.     For Each obj In Objects
  228.         obj.Draw canvas, r
  229.     Next obj
  230. End Sub
  231. Public Sub ClipEye(r As Single)
  232. Dim obj As Object
  233.  
  234.     For Each obj In Objects
  235.         If obj.ObjectType = "SOLID" Then _
  236.             obj.ClipEye r
  237.     Next obj
  238. End Sub
  239.  
  240. ' ************************************************
  241. ' Perform backface removal on the solids.
  242. ' ************************************************
  243. Public Sub Cull(X As Single, Y As Single, z As Single)
  244. Dim obj As Object
  245.  
  246.     For Each obj In Objects
  247.         If obj.ObjectType = "SOLID" Or _
  248.            obj.ObjectType = "TRANSFORMED" _
  249.         Then obj.Cull X, Y, z
  250.     Next obj
  251. End Sub
  252.  
  253. ' ************************************************
  254. ' Write the picture to a file using Write.
  255. ' Begin with TYPE_STRING to identify this object.
  256. ' ************************************************
  257. Sub FileWrite(filenum As Integer)
  258. Dim obj As Object
  259.  
  260.     Write #filenum, TYPE_STRING
  261.     Write #filenum, Objects.Count
  262.     
  263.     For Each obj In Objects
  264.         obj.FileWrite filenum
  265.     Next obj
  266. End Sub
  267.  
  268. ' ************************************************
  269. ' Apply a nonlinear transformation to the objects.
  270. ' ************************************************
  271. Sub Distort(trans As Object)
  272. Dim obj As Object
  273.  
  274.     For Each obj In Objects
  275.         obj.Distort trans
  276.     Next obj
  277. End Sub
  278.  
  279.  
  280. ' ************************************************
  281. ' Apply a transformation matrix which may not
  282. ' contain 0, 0, 0, 1 in the last column to the
  283. ' objects.
  284. ' ************************************************
  285. Sub ApplyFull(M() As Single)
  286. Dim obj As Object
  287.  
  288.     For Each obj In Objects
  289.         obj.ApplyFull M
  290.     Next obj
  291. End Sub
  292. ' ************************************************
  293. ' Apply a transformation matrix to the objects.
  294. ' ************************************************
  295. Sub Apply(M() As Single)
  296. Dim obj As Object
  297.  
  298.     For Each obj In Objects
  299.         obj.Apply M
  300.     Next obj
  301. End Sub
  302.  
  303.